package org.csu.mypetstore.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.CategoryMapper;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Locale;

@Service
public class CatalogService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    public Product getProduct( String productId) {
        return productMapper.getProduct(productId);
    }

    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.getProductListByCategory(categoryId);
    }

    public List<Product> getAllProducts() {
        return productMapper.getAllProducts();
    }

    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    public List<Product> searchProductById(String id) {
        return productMapper.searchProductById("%" + id.toLowerCase() + "%");
    }

    public List<Product> searchProductByIdAndCategory(String id, String category) {
        return productMapper.searchProductByIdAndCategory("%" + id.toLowerCase() + "%", category);
    }

    public List<Item> getItemListByProduct( String productId) {
        return itemMapper.getItemListByProduct(productId);
    }

    public Item getItem(String itemId) {
        return itemMapper.getItem(itemId);
    }

    public boolean isItemInStock(String itemId) {
        return itemMapper.getInventoryQuantity(itemId) > 0;
    }

    public void removeProduct(Product product) {
        productMapper.removeProduct(product);
    }

    public void removeItem(Item item) {
        itemMapper.removeItem(item);
        itemMapper.removeInventory(item);
    }

    public void UpdateItem(Item item) {
        itemMapper.updateQuantity(item);
        itemMapper.updateItem(item);
    }

    public void changeItemStatus(Item item) {
        if(item.getStatus().equals("P")) {
            itemMapper.statusToF(item);
        }
        else {
            itemMapper.statusToP(item);
        }
    }

    public void addItem(Item item) {
        itemMapper.addItem(item);
        itemMapper.addInventory(item);
    }

    public List<Product> getAllProductList(){
        return productMapper.getAllProducts();
    }

    public PageInfo<Product> getProductPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList = productMapper.getAllProducts();
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        return pageInfo;
    }

    public void updateProduct(Product product) {
        productMapper.updateProduct(product);
    }

    public void addProduct(Product product) {
        productMapper.addProduct(product);
    }

}
